package com.qf.staff.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.common.base.exception.ServiceException;
import com.qf.common.base.result.ResultCode;
import com.qf.common.db.utils.PageCommonBeanUtils;
import com.qf.staff.common.qo.DeptQo;
import com.qf.staff.common.vo.DeptVo;
import com.qf.staff.mapper.DeptMapper;
import com.qf.staff.mapper.EmpMapper;
import com.qf.staff.pojo.Dept;
import com.qf.staff.pojo.Emp;
import com.qf.staff.service.DeptSerice;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeptSericeImpl implements DeptSerice {
    @Resource
    DeptMapper deptMapper;
    @Resource
    EmpMapper empMapper;

    @Override
    public IPage<DeptVo> list(int page, int size) {
        try {
            return PageCommonBeanUtils.copyPage(deptMapper.selectPage(new Page<>(page, size), null),
                    new Page<>(), DeptVo::new);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.QUERY_ERROR);
        }
    }

    @Transactional
    @Override
    public int delete(DeptQo deptQo) {
        try {
            Integer deptId = deptQo.getDeptId();
            Integer status = deptQo.getStatus();
            QueryWrapper<Emp> qw = new QueryWrapper<>();
            qw.eq("dept_id", deptId);
            List<Emp> emps = empMapper.selectList(qw);
            for (Emp emp : emps) {
                emp.setStatus(status);
                empMapper.updateByStatus(emp);
            }
            Dept dept = new Dept();
            BeanUtils.copyProperties(deptQo, dept);
            return deptMapper.updateByStatus(dept);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public int modify(DeptQo deptQo) {
        try {
            Dept dept = new Dept();
            BeanUtils.copyProperties(deptQo, dept);
            return deptMapper.updateById(dept);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.UPDATE_ERROR);
        }
    }

    @Override
    public int add(DeptQo deptQo) {
        try {
            Dept dept = new Dept();
            BeanUtils.copyProperties(deptQo, dept);
            return deptMapper.insert(dept);
        } catch (BeansException e) {
            throw new ServiceException(ResultCode.INSERT_ERROR);
        }
    }

    @Transactional
    @Override
    public int deleteAll(List<Integer> ids, int status) {
        try {
            int num = deptMapper.updateByAllStatus(ids, status);
            if (num < ids.size()) {
                throw new ServiceException(ResultCode.DELETE_ERROR);
            }
            return num;
        } catch (Exception e) {
            throw new ServiceException(ResultCode.DELETE_ERROR);
        }
    }

    @Override
    public int count(String deptName) {
        try {
            return deptMapper.count(deptName);
        } catch (Exception e) {
            throw new ServiceException(ResultCode.SYS_ERROR);
        }
    }
}
